<?php
session_start();

include_once("connexion.php");


if ($_SESSION['log'] == 1) {
    if ($_GET['id']){
	$query = mysqli_fetch_array(mysqli_query($bdd, "SELECT * FROM biens WHERE  id=".$_GET['id']));
        
        $sql_photo="SELECT photo FROM photos";
        $req_photo= mysqli_query($bdd, $sql_photo);
        $Photo = mysqli_fetch_array($req_photo);
        
        $query_villes = "SELECT * FROM villes ";
        $req_ville = mysqli_query($bdd,$query_villes);
                                
        $query_pays = "SELECT * FROM pays ";
        $req_pays = mysqli_query($bdd,$query_pays);
       
}
if (isset($_POST["OK"])){
	$ville= $_POST["ville"];
        $pays = $_POST["pays"];
        $adresse = $_POST["adresse"];
	$codepostal = $_POST["codepostal"];
        $tarif_jour = $_POST["tarif_semaine"];
        $tarif_semaine = $_POST["tarif_semaine"];
	$surface = $_POST["surface"];
        $description = $_POST["description"];
	$query2 = "UPDATE biens SET 
		id_ville = '".$ville."',
                id_pays = '".$pays."',
                adresse = '".$adresse."',
		codepostal = '".$codepostal."',
		tarif_jour = '".$tarif_jour."',
                tarif_semaine = '".$tarif_semaine."',
		surface = '".$surface."',
		description = '".$description."'
		WHERE id = ".$_GET['id'];
                echo $query2;
	$resultat = mysqli_query($bdd,$query2);
        
	header('Location:mes_biens.php');
    }
}

if(isset($_FILES['img'])){
    $arrayImg = array('gif','jpg','jpeg','png');
    if ($_FILES['img']['size']>0){
        $uploadfile = "images/" . utf8_decode(basename($_FILES['img']['name']));
        $file_extension = pathinfo($uploadfile, PATHINFO_EXTENSION);

        if (!in_array($file_extension, $arrayImg)){
            $error = "Erreur lors de l'envoie de fichier";
        }
        if ($error != "" || !move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)){
          echo "erreur lors de l'envoiedu fichier";
        }else{
			  $query= "INSERT INTO photos (id, photo, id_bien, couverture) 
					  VALUES ('',
						  '".$uploadfile."',
						  '".$id_bien."',
						  '');";
						  
						  
			  $resultat = mysqli_query($bdd,$query) or die('Erreur SQL !'.$query.'</br>'.mysqli_error($bdd));
 
          /*insert ... */
          echo "Upload Ok";

        }
    }
  }
include_once("modifier_bien.html");

?>